/**
 * PLM项目简单工具配置文件
 * 用于导入项目中使用的基础工具
 */

// 导入模块解析助手
const moduleResolver = require('../helpers/module_resolver');

// 尝试导入日期处理工具
let dateHelper;
try {
  dateHelper = require('../helpers/date_helper');
} catch (error) {
  console.warn('无法导入日期助手，使用默认实现');
  dateHelper = {};
}

// 尝试导入校验工具
let validate;
try {
  validate = require('../helpers/validate_helper');
} catch (error) {
  console.warn('无法导入校验助手，使用默认实现');
  validate = (rule, value) => true;
}

// 尝试导入错误处理工具
let errorHelper;
try {
  errorHelper = require('../helpers/error_helper');
} catch (error) {
  console.warn('无法导入错误助手，使用默认实现');
  errorHelper = {
    logError: (error, context) => console.error(`[${context}]`, error),
    showError: (title, content) => wx.showToast({ title, icon: 'none' })
  };
}

// 尝试导入缓存工具
let cacheHelper;
try {
  cacheHelper = require('../helpers/cache_helper');
} catch (error) {
  console.warn('无法导入缓存助手，使用默认实现');
  cacheHelper = {
    get: (key, def) => {
      try {
        const value = wx.getStorageSync(`plm_${key}`);
        return value !== '' ? value : def;
      } catch (e) {
        return def;
      }
    },
    set: (key, value) => {
      try {
        wx.setStorageSync(`plm_${key}`, value);
        return true;
      } catch (e) {
        return false;
      }
    },
    remove: (key) => {
      try {
        wx.removeStorageSync(`plm_${key}`);
        return true;
      } catch (e) {
        return false;
      }
    }
  };
}

// 尝试导入时间处理工具
let timeHelper;
try {
  timeHelper = require('../helpers/time_helper');
} catch (error) {
  console.warn('无法导入时间助手，使用默认实现');
  timeHelper = {
    formatDateTime: (date) => new Date(date).toLocaleString(),
    formatDate: (date) => new Date(date).toLocaleDateString()
  };
}

// 尝试导入状态处理工具
let statusHelper;
try {
  statusHelper = require('../helpers/status_helper');
} catch (error) {
  console.warn('无法导入状态助手，使用默认实现');
  statusHelper = {
    getStatusLabel: (status) => status.toString(),
    getStatusClass: (status) => 'status-default'
  };
}

// 导入日志助手
let logHelper;
try {
  logHelper = require('../helpers/log_helper');
} catch (error) {
  console.warn('无法导入日志助手，使用默认实现');
  logHelper = {
    logEvent: (event, data) => console.log(`[事件] ${event}:`, data),
    logError: (error, context) => console.error(`[错误] ${context}:`, error),
    logPageView: (page, params) => console.log(`[页面] ${page}:`, params)
  };
}

// 导入网络助手
let networkHelper;
try {
  networkHelper = require('../helpers/network_helper');
} catch (error) {
  console.warn('无法导入网络助手，使用默认实现');
  networkHelper = {
    getNetworkStatus: async () => ({ isConnected: true, networkType: 'wifi' }),
    listenNetworkStatus: (callback) => {
      wx.onNetworkStatusChange(callback);
      return () => wx.offNetworkStatusChange();
    }
  };
}

module.exports = {
  dateHelper,
  validate,
  errorHelper,
  cacheHelper,
  timeHelper,
  statusHelper,
  logHelper,
  networkHelper
}; 